package cn.com.generaldata.os.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.service.common.AreaService;
import cn.com.generaldata.os.service.common.LAreaService;
import cn.com.generaldata.os.vo.AreaVO;
import cn.com.generaldata.os.vo.LAreaVO;
import cn.com.generaldata.os.ws.intf.AreaWSInterface;

@Component
@InjectName
public class AreaWS{

	@Autowired
	private AreaService areaService;
	
	@Autowired
	private LAreaService lareaService;

	
	@At("/area/queryAreasByParent/?")
	@Ok("json")
	public List<AreaVO> queryAreasByParent(String pid) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("id");
		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "areaCode", null));
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "parentId", pid));

		List<AreaVO> voList = toVoList(areaService.queryAreas(filter, null));
		return voList;
	}

	@At("/area/queryLAreasByParent/?")
	@Ok("json")
	public List<LAreaVO> queryLAreasByParent(String pid) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("lareaName");
		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "lareaCode", null));
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "parentId", pid));

		return toVoAList(lareaService.queryLAreas(filter, null));
	}

	
	public List<LAreaVO> queryLAreas() {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("lareaName");

		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "lareaCode", null));

		return toVoAList(lareaService.queryLAreas(filter, null));
	}

	
	public List<AreaVO> queryAreas() {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("id");

		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "areaCode", null));

		return toVoList(areaService.queryAreas(filter, null));
	}

	@At("/area/queryAreasById/?")
	@Ok("json")
	public List<AreaVO> queryAreasById(String areaId) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("id");
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "id", areaId));
		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "areaCode", null));
		Result queryAreas = areaService.queryAreas(filter, null);
		if (queryAreas != null) {
			List<cn.com.generaldata.os.pojo.Area> list = (List<cn.com.generaldata.os.pojo.Area>) queryAreas
					.getOperationResult();
			cn.com.generaldata.os.pojo.Area area = list.get(0);
			String hql = "from Area where areaCode is not null and (id='" + area.getParentId()
					+ "' or parentId='0') order by id";
			hql += "";
			Result queryAreas2 = areaService.queryAreasHql(hql, null, null);
			// Filter filter2 = new Filter();
			// filter2.addRule(new FilterRule(FilterConstant.ORVALUE, "parentId", list.get(0).getParentId(), "0"));
			// Result queryAreas2 = areaService.queryAreas(filter2, null);
			if (queryAreas2 != null) {
				list.addAll((List<cn.com.generaldata.os.pojo.Area>) queryAreas2.getOperationResult());
			}
			queryAreas.setOperationResult(list);
		}
		return toVoList(queryAreas);
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	private AreaVO toVo(cn.com.generaldata.os.pojo.Area pArea) {
		if (pArea != null) {
			return new AreaVO(pArea.getId(), pArea.getAreaName(), pArea.getAreaDesc(), pArea.getAreaCode(),
					pArea.getAreaZimuCode(), pArea.getParentId());
		}
		return null;
	}

	private LAreaVO toVoA(cn.com.generaldata.os.pojo.LArea pArea) {
		if (pArea != null) {
			return new LAreaVO(pArea.getId(), pArea.getLareaName(), pArea.getLareaDesc(), pArea.getLareaCode(),
					pArea.getLareaZimuCode(), pArea.getParentId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<LAreaVO> toVoAList(cn.com.generaldata.os.paging.Result pResult) {
		List<LAreaVO> areas = new ArrayList<LAreaVO>();
		for (cn.com.generaldata.os.pojo.LArea pArea : (List<cn.com.generaldata.os.pojo.LArea>) pResult
				.getOperationResult()) {
			areas.add(toVoA(pArea));
		}
		return areas;
	}

	@SuppressWarnings("unchecked")
	private List<AreaVO> toVoList(cn.com.generaldata.os.paging.Result pResult) {
		List<AreaVO> areas = new ArrayList<AreaVO>();
		for (cn.com.generaldata.os.pojo.Area pArea : (List<cn.com.generaldata.os.pojo.Area>) pResult
				.getOperationResult()) {
			areas.add(toVo(pArea));
		}
		return areas;
	}

}
